<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="theme-color" content="#0078E7"><meta name="author" content="zzl"><meta name="copyright" content="zzl"><meta name="generator" content="Hexo 5.4.0"><meta name="theme" content="hexo-theme-yun"><title>python导入Excel表格数据 | zzl</title><link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@900&amp;display=swap" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/star-markdown-css@0.1.25/dist/yun/yun-markdown.min.css"><script src="//at.alicdn.com/t/font_1140697_dxory92pb0h.js" async></script><script src="https://cdn.jsdelivr.net/npm/scrollreveal/dist/scrollreveal.min.js" defer></script><script>document.addEventListener("DOMContentLoaded", () => {
  [".post-card",".post-content img"].forEach((target)=> {
    ScrollReveal().reveal(target);
  })
});
</script><link rel="icon" href="/web/yun.svg"><link rel="mask-icon" href="/web/yun.svg" color="#0078E7"><link rel="alternate icon" href="/web/yun.ico"><link rel="preload" href="/web/css/hexo-theme-yun.css" as="style"><link rel="preload" href="/web/js/utils.js" as="script"><link rel="preload" href="/web/js/hexo-theme-yun.js" as="script"><link rel="prefetch" href="/web/js/sidebar.js" as="script"><link rel="preconnect" href="https://cdn.jsdelivr.net" crossorigin><script id="yun-config">
    const Yun = window.Yun || {};
    window.CONFIG = {"hostname":"zilongzeng.gitee.io","root":"/web/","title":"奏者的小站","version":"1.6.2","mode":"auto","copycode":true,"page":{"isPost":true},"i18n":{"placeholder":"Searching...","empty":"We didn't find any results for the search: ${query}.","hits":"${hits} results found","hits_time":"${hits} results found in ${time} ms"},"anonymous_image":"https://cdn.jsdelivr.net/gh/YunYouJun/cdn/img/avatar/none.jpg","say":{"api":"https://v1.hitokoto.cn","hitokoto":true},"fireworks":{"colors":["102, 167, 221","62, 131, 225","33, 78, 194"]}};
  </script><link rel="stylesheet" href="/web/css/hexo-theme-yun.css"><script src="/web/js/utils.js"></script><script src="/web/js/hexo-theme-yun.js"></script><meta name="description" content="最近学习python数据挖掘中遇到了需要从excel读取数据并保存成array情况">
<meta property="og:type" content="article">
<meta property="og:title" content="python导入Excel表格数据">
<meta property="og:url" content="http://zilongzeng.gitee.io/web/2021/09/11/python%E5%AF%BC%E5%85%A5Excel%E8%A1%A8%E6%A0%BC%E6%95%B0%E6%8D%AE/index.html">
<meta property="og:site_name" content="zzl">
<meta property="og:description" content="最近学习python数据挖掘中遇到了需要从excel读取数据并保存成array情况">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://i.loli.net/2021/09/12/2gmYxFhluULC4jM.png">
<meta property="og:image" content="https://i.loli.net/2021/09/12/2nF1ONhBuHwbiJP.png">
<meta property="og:image" content="https://i.loli.net/2021/09/12/UamBFIwylQfZo2i.png">
<meta property="og:image" content="https://i.loli.net/2021/09/12/MUAuRpZgLYwz5J1.jpg">
<meta property="article:published_time" content="2021-09-11T11:10:52.000Z">
<meta property="article:modified_time" content="2022-01-16T09:02:33.635Z">
<meta property="article:author" content="zzl">
<meta property="article:tag" content="Python">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://i.loli.net/2021/09/12/2gmYxFhluULC4jM.png"><script src="/web/js/ui/mode.js"></script></head><body><script defer src="https://cdn.jsdelivr.net/npm/animejs@latest"></script><script defer src="/web/js/ui/fireworks.js"></script><canvas class="fireworks"></canvas><div class="container"><a class="sidebar-toggle hty-icon-button" id="menu-btn"><div class="hamburger hamburger--spin" type="button"><span class="hamburger-box"><span class="hamburger-inner"></span></span></div></a><div class="sidebar-toggle sidebar-overlay"></div><aside class="sidebar"><script src="/web/js/sidebar.js"></script><ul class="sidebar-nav"><li class="sidebar-nav-item sidebar-nav-toc hty-icon-button" data-target="post-toc-wrap" title="Table of Contents"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-list-ordered"></use></svg></li><li class="sidebar-nav-item sidebar-nav-overview hty-icon-button sidebar-nav-active" data-target="site-overview-wrap" title="Overview"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-passport-line"></use></svg></li></ul><div class="sidebar-panel sidebar-panel-active" id="site-overview-wrap"><div class="site-info fix-top"><a class="site-author-avatar" href="/web/about/" title="zzl"><img width="96" loading="lazy" src="/web/zzl.jpg" alt="zzl"><span class="site-author-status" title="永远相信美好的事情即将发生">😊</span></a><div class="site-author-name"><a href="/web/about/">zzl</a></div><span class="site-name">zzl</span><sub class="site-subtitle"></sub><div class="site-desciption"></div></div><nav class="site-state"><a class="site-state-item hty-icon-button icon-home" href="/web/" title="Home"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-home-4-line"></use></svg></span></a><div class="site-state-item"><a href="/web/archives/" title="Archives"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-archive-line"></use></svg></span><span class="site-state-item-count">16</span></a></div><div class="site-state-item"><a href="/web/categories/" title="Categories"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-folder-2-line"></use></svg></span><span class="site-state-item-count">0</span></a></div><div class="site-state-item"><a href="/web/tags/" title="Tags"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-price-tag-3-line"></use></svg></span><span class="site-state-item-count">9</span></a></div><a class="site-state-item hty-icon-button" target="_blank" rel="noopener" href="https://yun.yunyoujun.cn" title="文档"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-settings-line"></use></svg></span></a></nav><hr style="margin-bottom:0.5rem"><div class="links-of-author"><a class="links-of-author-item hty-icon-button" rel="noopener" href="/atom.xml" title="RSS" target="_blank" style="color:orange"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-rss-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="QQ 2915667803" target="_blank" style="color:#12B7F5"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-qq-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="GitHub" target="_blank" style="color:#6e5494"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-github-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="微博" target="_blank" style="color:#E6162D"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-weibo-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="豆瓣" target="_blank" style="color:#007722"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-douban-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="网易云音乐" target="_blank" style="color:#C20C0C"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-netease-cloud-music-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="知乎" target="_blank" style="color:#0084FF"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-zhihu-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="哔哩哔哩" target="_blank" style="color:#FF8EB3"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-bilibili-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="微信公众号" target="_blank" style="color:#1AAD19"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-wechat-2-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="Twitter" target="_blank" style="color:#1da1f2"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-twitter-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="Telegram Channel" target="_blank" style="color:#0088CC"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-telegram-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="E-Mail" target="_blank" style="color:#8E71C1"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-mail-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="Travelling" target="_blank" style="color:var(--hty-text-color)"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-train-line"></use></svg></a></div><hr style="margin:0.5rem 1rem"><div class="links"><a class="links-item hty-icon-button" href="/web/links/" title="我的小伙伴们" style="color:dodgerblue"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-genderless-line"></use></svg></a></div><br><a class="links-item hty-icon-button" id="toggle-mode-btn" href="javascript:;" title="Mode" style="color: #f1cb64"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-contrast-2-line"></use></svg></a></div><div class="sidebar-panel" id="post-toc-wrap"><div class="post-toc"><div class="post-toc-content">很遗憾，咱没写啥目录</div></div></div></aside><main class="sidebar-translate" id="content"><div id="post"><article class="hty-card post-block" itemscope itemtype="https://schema.org/Article"><link itemprop="mainEntityOfPage" href="http://zilongzeng.gitee.io/web/web/2021/09/11/python%E5%AF%BC%E5%85%A5Excel%E8%A1%A8%E6%A0%BC%E6%95%B0%E6%8D%AE/"><span hidden itemprop="author" itemscope itemtype="https://schema.org/Person"><meta itemprop="name" content="zzl"><meta itemprop="description"></span><span hidden itemprop="publisher" itemscope itemtype="https://schema.org/Organization"><meta itemprop="name" content="zzl"></span><header class="post-header"><h1 class="post-title" itemprop="name headline">python导入Excel表格数据</h1><div class="post-meta"><div class="post-time" style="display:inline-block"><span class="post-meta-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-calendar-line"></use></svg></span> <time title="Created: 2021-09-11 19:10:52" itemprop="dateCreated datePublished" datetime="2021-09-11T19:10:52+08:00">2021-09-11</time><span class="post-meta-divider">-</span><span class="post-meta-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-calendar-2-line"></use></svg></span> <time title="Modified: 2022-01-16 17:02:33" itemprop="dateModified" datetime="2022-01-16T17:02:33+08:00">2022-01-16</time></div><div class="post-classify"><span class="post-tag"><a class="tag-item" href="/web/tags/Python/" style="--text-color:#4776ab"><span class="post-meta-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-price-tag-3-line"></use></svg></span><span class="tag-name">Python</span></a></span></div></div></header><section class="post-body" itemprop="articleBody"><div class="post-content markdown-body" style="--smc-primary:#0078E7;"><blockquote>
<p>一般情况下我们用<code>xlrd</code>包从<code>excel</code>中读取的数据无法直接用于数据分析或制作图表，读取代码如下：</p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> xlrd  <span class="comment">#导入xlrd包</span></span><br><span class="line">x1 = xlrd.open_workbook(<span class="string">r&#x27;test.xlsx&#x27;</span>) <span class="comment">#读取test.xlsx文件</span></span><br><span class="line">table = x1.sheets()[<span class="number">0</span>] <span class="comment">#通过索引获取表</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<blockquote>
<p>若按上面方法读取你会发现<code>x1</code>的类型是<code>book</code>，<code>table</code>的类型是<code>sheet</code>。</p>
</blockquote>
<p><img src="https://i.loli.net/2021/09/12/2gmYxFhluULC4jM.png" alt="python210911-2.png" loading="lazy"></p>
<blockquote>
<p>这时我们需要将<code>sheet</code>类型的数据转换成<code>array</code>类型,完全代码如下：</p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br><span class="line"><span class="keyword">import</span> xlrd</span><br><span class="line">resArray = []  <span class="comment">#声明一个空list</span></span><br><span class="line">x1 = xlrd.open_workbook(<span class="string">r&#x27;E:\AAjava\test.xlsx&#x27;</span>)   <span class="comment">#读取文件</span></span><br><span class="line">table = x1.sheets()[<span class="number">0</span>]  <span class="comment">#通过索引获取表</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(table.nrows):  <span class="comment">#按行进行循环，table.nrows表示总行数</span></span><br><span class="line">    line = table.row_values(i)  <span class="comment">#读取每行数据并保存在line里</span></span><br><span class="line">    resArray.append(line)  <span class="comment">#将line加入到resArray中，此时的resArray是二维list</span></span><br><span class="line">resArray = np.array(resArray) <span class="comment">#将resArray从二维list变成数组</span></span><br><span class="line"><span class="built_in">print</span>(resArray)</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<blockquote>
<p>上述代码中加入<code>resArray = np.array(resArray)</code>与未加入的区别，也就是二维<code>list</code>与数组的区别，下面我将图贴上，大家自行比较</p>
</blockquote>
<p><img src="https://i.loli.net/2021/09/12/2nF1ONhBuHwbiJP.png" alt="python210911-3.png" loading="lazy"></p>
<p><img src="https://i.loli.net/2021/09/12/UamBFIwylQfZo2i.png" alt="python210911-4.png" loading="lazy"></p>
<p><img src="https://i.loli.net/2021/09/12/MUAuRpZgLYwz5J1.jpg" alt="zzl.jpg" loading="lazy"></p>
</div><div id="reward-container"><span class="hty-icon-button button-glow" id="reward-button" title="Donate" onclick="var qr = document.getElementById(&quot;qr&quot;); qr.style.display = (qr.style.display === &quot;none&quot;) ? &quot;block&quot; : &quot;none&quot;;"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-hand-coin-line"></use></svg></span><div id="reward-comment">I want you.</div><div id="qr" style="display:none;"><div style="display:inline-block"></div><div style="display:inline-block"></div><div style="display:inline-block"></div></div></div><ul class="post-copyright"><li class="post-copyright-author"><strong>Post author: </strong>zzl</li><li class="post-copyright-link"><strong>Post link: </strong><a href="http://zilongzeng.gitee.io/web/2021/09/11/python%E5%AF%BC%E5%85%A5Excel%E8%A1%A8%E6%A0%BC%E6%95%B0%E6%8D%AE/" title="python导入Excel表格数据">http://zilongzeng.gitee.io/web/2021/09/11/python%E5%AF%BC%E5%85%A5Excel%E8%A1%A8%E6%A0%BC%E6%95%B0%E6%8D%AE/</a></li><li class="post-copyright-license"><strong>Copyright Notice: </strong>All articles in this blog are licensed under <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" target="_blank" rel="noopener" title="CC BY-NC-SA 4.0 "><svg class="icon"><use xlink:href="#icon-creative-commons-line"></use></svg><svg class="icon"><use xlink:href="#icon-creative-commons-by-line"></use></svg><svg class="icon"><use xlink:href="#icon-creative-commons-nc-line"></use></svg><svg class="icon"><use xlink:href="#icon-creative-commons-sa-line"></use></svg></a> unless otherwise stated.</li></ul></section></article><div class="post-nav"><div class="post-nav-item"><a class="post-nav-prev" href="/web/2021/09/12/Image-Upload/" rel="prev" title="Image Upload"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-arrow-left-s-line"></use></svg><span class="post-nav-text">Image Upload</span></a></div><div class="post-nav-item"><a class="post-nav-next" href="/web/2021/09/09/%E7%AE%80%E4%BB%8B/" rel="next" title="简介"><span class="post-nav-text">简介</span><svg class="icon" aria-hidden="true"><use xlink:href="#icon-arrow-right-s-line"></use></svg></a></div></div></div><div class="hty-card" id="comment"><div class="comment-tooltip text-center"><span>要不要和我说些什么？</span><br></div></div></main><footer class="sidebar-translate" id="footer"><div class="copyright"><span>&copy; 2019 – 2022 </span><span class="with-love" id="animate"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-cloud-line"></use></svg></span><span class="author"> zzl</span></div><div class="powered"><span>Powered by <a href="https://hexo.io" target="_blank" rel="noopener">Hexo</a> v5.4.0</span><span class="footer-separator">|</span><span>Theme - <a rel="noopener" href="https://github.com/YunYouJun/hexo-theme-yun" target="_blank"><span>Yun</span></a> v1.6.2</span></div></footer><a class="hty-icon-button" id="back-to-top" aria-label="back-to-top" href="#"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-arrow-up-s-line"></use></svg><svg class="progress-circle-container" viewBox="0 0 100 100"><circle class="progress-circle" id="progressCircle" cx="50" cy="50" r="48" fill="none" stroke="#0078E7" stroke-width="2" stroke-linecap="round"></circle></svg></a></div></body></html>